package suggestie1;



import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.util.*;
public class CanvasTest extends JPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public CanvasTest() {

	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	}

	public static void main(String[] args)
	{ 
		// Change this next part later to be dynamic.
		int canvasSize = 500;
		int pixelsize = 20;
		int rowlength = 480/pixelsize;
		JFrame frame = new JFrame();
		JLabel label = new JLabel();
		BufferedImage bImage = new BufferedImage(canvasSize, canvasSize, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = bImage.createGraphics();
		ImageIcon iIcon = new ImageIcon(bImage); 
		label.setIcon(iIcon);
		g2d.setColor(Color.white);
		g2d.setBackground(Color.white);
		frame.add(label);
		frame.setPreferredSize(new Dimension(500,500));
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		g2d.fillRect(0, 0, 500, 500);
		g2d.setColor(Color.black);
		
		// Triangle drawing
		
		Triangle tri = new Triangle(60,60,180,180,300,60);
		Triangle tri2 = new Triangle(60,60,180,180,60,300);
		Triangle tri3 = new Triangle(300,300,180,180,300,60);
		Triangle tri4 = new Triangle(300,300,180,180,60,300);
		rasterize(label, g2d, tri, pixelsize, rowlength ,Color.black,Color.red);
		rasterize(label, g2d, tri2, pixelsize, rowlength,Color.white,Color.blue);
		rasterize(label, g2d, tri3, pixelsize, rowlength,Color.white,Color.green);
		rasterize(label, g2d, tri4, pixelsize, rowlength,Color.white,Color.pink);


	}
	public static void rasterize(JLabel label,Graphics2D g2d, Triangle tri, int pixelsize, int rowlength, Color color1, Color color2) {
		for(int i=tri.getSpans().elementAt(0)/pixelsize-1; i<=tri.getSpans().elementAt(1)/pixelsize-1; i++) {
			for(int j=tri.getSpans().elementAt(2)/pixelsize-1; j<=tri.getSpans().elementAt(3)/pixelsize-1; j++) {
				int x=pixelsize*(i+1)+pixelsize/2;
				int y=pixelsize*(j+1)+pixelsize/2;
				Point p = new Point(x,y);
				//g2d.clearRect(0, 0, 500, 500);
				if(pixelsize>2){
					g2d = drawPixel(g2d, p, (int) Math.ceil(pixelsize/4), color2);
				}
				if(tri.rasterize(p)) {
					g2d = drawPixel(g2d,p,pixelsize,color2);
				}
				if(pixelsize>4) {
					g2d = drawGrid(g2d, pixelsize, rowlength);
					g2d = drawTriangle(g2d, tri);
				}
				label.repaint();
				try {
					Thread.sleep(10L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static Graphics2D drawGrid(Graphics2D g2d, int pixelsize, int rowlength) {
		for(int i=0; i<=rowlength-1; i++) {
			g2d.drawLine(pixelsize,pixelsize+pixelsize*i,rowlength*pixelsize,pixelsize+pixelsize*i);
			g2d.drawLine(pixelsize+pixelsize*i,rowlength*pixelsize,pixelsize+pixelsize*i,pixelsize);
		}
		return g2d;
	}
	public static Graphics2D drawTriangle(Graphics2D g2d, Triangle tri) {
		ArrayList<Point> a = tri.getCoordinates();
		g2d.drawLine((int)a.get(0).getX(), (int)a.get(0).getY(), (int)a.get(1).getX(), (int)a.get(1).getY());
		g2d.drawLine((int)a.get(1).getX(), (int)a.get(1).getY(), (int)a.get(2).getX(), (int)a.get(2).getY());
		g2d.drawLine((int)a.get(2).getX(), (int)a.get(2).getY(), (int)a.get(0).getX(), (int)a.get(0).getY());
		return g2d;
	}

	public static Graphics2D drawPixel(Graphics2D g2d, Point p, int pixelsize, Color color) {
		g2d.setColor(color);
		int x = (int)p.getX();
		int y = (int)p.getY();
		g2d.fillRect(x-pixelsize/2, y-pixelsize/2, pixelsize, pixelsize);
		g2d.setColor(Color.black);
		return g2d;
	}
	

}